package cn.onlineclass.controller;

import cn.onlineclass.entity.OVideo;
import cn.onlineclass.service.videoService;
import cn.onlineclass.util.AjaxPage;
import cn.onlineclass.util.AjaxResult;
import cn.onlineclass.util.StringUtil;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class VideoController {
    public AjaxPage search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"), 1);
        String title= request.getParameter("title");
        String bookid=request.getParameter("bookid");
        AjaxPage p = videoService.TYPE.search(pageNo, 10,title,bookid);
//        String str = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNonStringValueAsString);
//        response.getWriter().print(str);
        return p;
    }

    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String title = request.getParameter("title");
        String url = request.getParameter("url");
        Boolean inuse = Boolean.valueOf((request.getParameter("inuse")));
        String coverimg=request.getParameter("coverimg");
        long bookid= Long.parseLong(request.getParameter("bookid"));
        OVideo c = new OVideo();
        c.setTitle(title);
        c.setUrl(url);
        c.setInuse(inuse);
        c.setBookid(bookid);
        c.setCoverimg(coverimg);
        videoService con = new videoService();
        int num = con.add(c);
        if (num>0){
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.error("保存失败");
    }
    public void toupdate(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toInt(request.getParameter("id"));
        videoService s = new videoService();
        OVideo query = s.get(id);
        String str = JSON.toJSONString(query);
        response.getWriter().print(str);
    }

    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id= Long.parseLong(request.getParameter("id"));
        String title = request.getParameter("title");
        String url = request.getParameter("url");
        boolean inuse = Boolean.parseBoolean((request.getParameter("inuse")));
        String coverimg=request.getParameter("coverimg");
        long bookid= Long.parseLong(request.getParameter("bookid"));
        OVideo c = new OVideo();
        c.setTitle(title);
        c.setUrl(url);
        c.setInuse(inuse);
        c.setBookid(bookid);
        c.setCoverimg(coverimg);
        c.setId(id);
        videoService con = new videoService();
        int num = con.update(c);
        if (num>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        OVideo c = new OVideo();
        int id = StringUtil.toInt(request.getParameter("id"));
        c.setId(id);
        c.setInuse(false);
        videoService con = new videoService();
        int num = con.del(c);
        if (num>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }
}
